#include "GMServerBroadcast.h"
#include "GameMessageFactory.h"
#include "ServerSocket.h"
#include "GMPrintText.h"

const char* GMServerBroadcast::m_NAME = "ServerBroadcast"; 
static bool registered = TheGameMessageFactory::Instance()->AddObject(GMServerBroadcast::m_NAME, GMServerBroadcast::Create);
GMServerBroadcast::GMServerBroadcast()
{}
GMServerBroadcast::GMServerBroadcast(std::string s)
{
	m_Msg = s;
}

GMServerBroadcast::~GMServerBroadcast()
{}

void GMServerBroadcast::Execute()
{
	//server broadcast
	GMPrintText gpt(m_Msg.c_str());
	SockMessage OutMsg;
	gpt.WriteToSocketM(&OutMsg);
	TheServer::Instance()->Broadcast(OutMsg);

}

bool GMServerBroadcast::ReadFromSocketM(SockMessage* sm)
{
	std::cout<<"Reading message: "<<m_NAME<<"\n";
	sm->ReadString(&m_Msg);
	return true;
}
bool GMServerBroadcast::WriteToSocketM(SockMessage* sm)
{
	std::cout<< "Writing message: "<<m_NAME <<"\n";
	sm->WriteString(m_Msg);
	return true;
}

GameMessage* GMServerBroadcast::Create()
{
	return new GMServerBroadcast;
}